﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using MovieStorage;
using DVDFilmoteka.SaveloadManagement.ImportFilters;
using System.Windows.Forms;
using DVDFilmoteka.src.SaveloadManagement.ImportFilters;

namespace DVDFilmoteka.SaveloadManagement
{
    class Loader
    {
        /// <summary>
        /// Otevre zadany soubor
        /// </summary>
        /// <param name="version"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public List<MovieDataStore12> load(String fileName, out Boolean upgraded)
        {
            StreamReader lStream = new StreamReader(fileName);            

            Version fileVersion = new Version("1.0");

            try
            {
                // Nacist verzi ze souboru
                String versionString = lStream.ReadLine();
                if (versionString.Contains("<Version>") && versionString.Contains("</Version"))
                {
                    versionString = versionString.Substring(9);
                    versionString = versionString.Substring(0, versionString.IndexOf('<'));
                    try
                    {
                        fileVersion = new Version(versionString);
                    }
                    catch { }
                }
                
                // Nacita verzi souboru 1.0 a mensi
                if (fileVersion.Equals(new Version("1.0")))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<MovieDataStore>));
                    List<MovieDataStore> filmList = (List<MovieDataStore>)serializer.Deserialize(lStream);
                    lStream.Close();

                    upgraded = true;
                    try
                    {
                        File.Copy(fileName, fileName + ".bak10");
                    }
                    catch { }
                    return Import12.import(Import11.import(filmList));
                }

                // Nacita verzi souboru 1.1
                if (fileVersion.Equals(new Version("1.1")))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<MovieDataStore>));
                    List<MovieDataStore> filmList = (List<MovieDataStore>)serializer.Deserialize(lStream);
                    lStream.Close();

                    upgraded = true;
                    try
                    {
                        File.Copy(fileName, fileName + ".bak11");
                    }
                    catch { }
                    return Import12.import(filmList);
                }

                // Nacita verzi souboru 1.2
                if (fileVersion.Equals(new Version("1.2")))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<MovieDataStore12>));
                    List<MovieDataStore12> filmList = (List<MovieDataStore12>)serializer.Deserialize(lStream);
                    lStream.Close();

                    upgraded = false;

                    foreach (var item in filmList)
                    {
                        if (item.mainGenre.ToLower().Equals("pohádky"))
                        {
                            item.mainGenre = "Pohádka"; // CSFD Zmenily zanr
                            upgraded = true;
                        }
                    }
                    
                    return filmList;
                }

                throw new Exception("Verze souboru je vyšší než verze aplikace, upgradujte na nejnovější verzi aplikace DVDFilmotéka.");
            }
            catch (Exception)
            {
                throw new Exception("Soubor má chybný formát nebo nelze řádně otevřít.");                
            }
            finally
            {
                lStream.Close();
            }            
        }
    }
}